﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyVoteMVC.BackstageManagement.Controllers;
using MyVoteMVC.Domain;


namespace MyVoteMVC.BackstageManagement.Views
{
    public partial class OffLineCounter : Form,IOffLineCounterView
    {
        private IOffLineCounterController controller;

        public OffLineCounter()
        {
            InitializeComponent();
            controller = new OffLineCounterController(this);
        }

        public void GetActivitysComplete(IList<VT_Activities> activities)
        {
            gvActivity.DataSource = activities;
        }

        public void GetQuestionsComplete(IList<VT_Questions> questions)
        {
            gvQuestion.DataSource = questions;
        }

        public void GetItemsComplete(IList<VT_Items> items)
        {
            gvItem.DataSource = items;
        }

        public void SetLineCounterComplete(string message, string questionId)
        {
            MessageBox.Show(message);
            controller.GetItems(questionId);
        }

        public void GetLineCounterComplete(int offLineCountValue,int onLineCountValue)
        {
            lblOnlineCount.Text = onLineCountValue.ToString();
            numOffLineCount.Value = offLineCountValue;
        }

        private void gvActivity_SelectionChanged(object sender, EventArgs e)
        {
            if (gvActivity.SelectedRows.Count > 0 && gvActivity.SelectedRows[0].DataBoundItem != null)
            {
                VT_Activities activity = gvActivity.SelectedRows[0].DataBoundItem as VT_Activities;
                controller.GetQuestions(activity.Activity_ID);
            }
            else
            {
                gvQuestion.DataSource = null;
            }
        }

        private void gvQuestion_SelectionChanged(object sender, EventArgs e)
        {
            if (gvQuestion.SelectedRows.Count > 0 && gvQuestion.SelectedRows[0].DataBoundItem != null)
            {
                VT_Questions question = gvQuestion.SelectedRows[0].DataBoundItem as VT_Questions;
                controller.GetItems(question.Question_ID);
            }
            else
            {
                gvItem.DataSource = null;
            }
        }

        private void gvItem_SelectionChanged(object sender, EventArgs e)
        {
            if (gvQuestion.SelectedRows.Count > 0 && gvQuestion.SelectedRows[0].DataBoundItem != null)
            {
                VT_Questions question = gvQuestion.SelectedRows[0].DataBoundItem as VT_Questions;
                if (gvItem.SelectedRows.Count > 0 && gvItem.SelectedRows[0].DataBoundItem != null)
                {
                    VT_Items item = gvItem.SelectedRows[0].DataBoundItem as VT_Items;
                    controller.GetLineCounter(question.Question_ID, item.Item_ID);
                }
                else
                {
                    numOffLineCount.Value = 0;
                }
            }
            else
            {
                numOffLineCount.Value = 0;
            }
        }

        private void OffLineCounter_Load(object sender, EventArgs e)
        {
            numOffLineCount.Maximum = int.MaxValue;
            controller.GetActivities();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
             if (gvQuestion.SelectedRows.Count > 0 && gvQuestion.SelectedRows[0].DataBoundItem != null)
            {
                VT_Questions question = gvQuestion.SelectedRows[0].DataBoundItem as VT_Questions;
                if (gvItem.SelectedRows.Count > 0 && gvItem.SelectedRows[0].DataBoundItem != null)
                {
                    VT_Items item = gvItem.SelectedRows[0].DataBoundItem as VT_Items;
                    controller.SetLineCounter(question.Question_ID, item.Item_ID, (int)numOffLineCount.Value);
                }
                else
                {
                    MessageBox.Show("请选中要设置的选项");
                    return;
                }
            }
            else
            {
                MessageBox.Show("请选中要设置的问题");
                return;
            }
           
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtKeyWord.Text) && gvQuestion.SelectedRows.Count > 0 && gvQuestion.SelectedRows[0].DataBoundItem != null)
            {
                VT_Questions question = gvQuestion.SelectedRows[0].DataBoundItem as VT_Questions;
                controller.GetItems(question.Question_ID, txtKeyWord.Text);
            }
            
        }
    }
}
